/**
 * Vosao CMS. Simple CMS for Google App Engine.
 * Copyright (C) 2009 Vosao development team
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * email: vosao.dev@gmail.com
 */

var config = null;

$(function(){
	
    $("#tabs").tabs();
    Vosao.initJSONRpc(loadData);
    $('#configForm').submit(function() {onSave(); return false;});
    $('#cancelTemplateButton, #cancelButton').click(function() {
    	location.href = "/cms/plugins/config.vm";
    })
    localize();;
});

function loadData() {
	loadConfig();
	//loadRegistrations();
}

function loadConfig() {
	Vosao.jsonrpc.csdataBackService.getConfig(function(r) {
		config = r;
		showConfig();
	});
}

function showConfig() {
	//$('#adminEmail').val(config.adminEmail);
	//$('#sendConfirmAdmin').each(function() {this.checked = config.sendConfirmAdmin});
	//$('#sendConfirmUser').each(function() {this.checked = config.sendConfirmUser});
	//$('#clearDays').val(config.clearDays);
	$('#csdataFormTemplate').val(config.csdataFormTemplate);
	//$('#confirmUserTemplate').val(config.confirmUserTemplate);
	//$('#confirmAdminTemplate').val(config.confirmAdminTemplate);
	//$('#captcha').each(function() {this.checked = config.captcha});
}

function validate(vo) {
	//if (!vo.adminEmail) {
	//	return messages('register.admin_email_empty');
	//}
	//if (isNaN(parseInt(vo.clearDays))) {
	//	return messages('register.clear_days_nan');
	//}
	if (!vo.csdataFormTemplate) {
		return messages('csdata.template_empty');
	}
	//if (!vo.confirmUserTemplate) {
	//	return messages('register.confirm_user_empty');
	//}
	//if (!vo.confirmAdminTemplate) {
	//	return messages('register.confirm_admin_empty');
	//}
}

function onSave() {
	var vo = {
		//adminEmail : $('#adminEmail').val(),
		//sendConfirmAdmin : String($('#sendConfirmAdmin:checked').size() > 0),
		//sendConfirmUser : String($('#sendConfirmUser:checked').size() > 0),
		//clearDays : $('#clearDays').val(),
		csdataFormTemplate : $('#csdataFormTemplate').val(),
		//confirmUserTemplate : $('#confirmUserTemplate').val(),
		//captcha : String($('#captcha:checked').size() > 0),
		//confirmAdminTemplate : $('#confirmAdminTemplate').val()		
	};
	var error = validate(vo);
	if (error) {
		Vosao.error(error);
	}
	else {
		Vosao.jsonrpc.csdataBackService.saveConfig(function(r) {
			Vosao.showServiceMessages(r);
		}, Vosao.javaMap(vo));
	}
}

//function loadRegistrations() {
//	Vosao.jsonrpc.registerBackService.getRegistrations(function(r) {
//		registrations = r.list;
//		showRegistrations();
//	});
//}

//function showRegistrations() {
//	var h = '<table class="form-table"><tr><th>' + messages('register.date')
//	    + '</th><th>' + messages('email') + '</th><th>' + messages('name') 
//	    + '</th><th></th></tr>';
//	$.each(registrations, function(i,value) {
//		h += '<tr><td>' + value.createdDateString + '</td><td>' 
//			+ value.email + '</td><td>' + value.name 
//			+ '</td><td><a href="#" onclick="onConfirm(' + i 
//			+ ')" title="Confirm"><img src="/static/images/02_plus.png" /></a>'
//			+ '<a href="#" onclick="onRemove(' + i + ')" title="' 
//			+ messages('remove') + '"><img src="/static/images/02_x.png" /></a></td></tr>';
//	});
//	$('#registrations').html(h + '</table>');
//}





function onFormTemplateRestore() {
	alert("Entro onFromTemplateRestore");
	Vosao.jsonrpc.csdataBackService.restoreCsdataFormTemplate(function(r) {
		if (r.result == 'success') {
			$('#csdataFormTemplate').val(r.message);
			Vosao.info(messages('success'));
		}
		else {
			Vosao.showServiceMessages(r);
		}
	});
}


function localize() {
	document.title = messages('csdata.plugin_config');
	$('#leftmenu a:eq(1)').text(messages('content'));
	$('#leftmenu a:eq(2)').text(messages('templates'));
	$('#leftmenu a:eq(3)').text(messages('resources'));
	$('#leftmenu a:eq(4)').text(messages('configuration'));
	$('#leftmenu a:eq(5)').text(messages('plugins'));
	$('#leftmenu a:eq(6)').text(messages('plugins.config'));

	$('#rightmenu a:eq(0)').text(messages('profile'));
	$('#rightmenu a:eq(1)').text(messages('logout'));
	$('#languageSelect').text(messages('language'));
	$('#rightmenu a:contains(support)').text(messages('support'));
	
	$('#tabs ul li:eq(0) a').text(messages('csdata.tab1'));
	$('#tabs ul li:eq(1) a').text(messages('templates'));
	
	//$('#tab-1 div:eq(0) label').text(messages('register.admin_email'));
	//$('#tab-1 div:eq(1) label').text(messages('register.send_confirm_admin'));
	//$('#tab-1 div:eq(2) label').text(messages('register.send_confirm_user'));
	//$('#tab-1 div:eq(3) label').text(messages('register.clear_days'));
	//$('#tab-1 div:eq(4) label').text(messages('register.enable_captcha'));

	$('input[value=save]').val(messages('save'));
	$('input[value=cancel]').val(messages('cancel'));
	
	$('#tab-2 > div:eq(0) span').html(messages('csdata.registration_form'));
	$('#tab-2 a').text(messages('restore'));
	//$('#tab-2 > div:eq(1) span').html(messages('csdata.confirm_user_letter'));
	//$('#tab-2 > div:eq(2) span').html(messages('register.confirm_admin_letter'));
}